package com.qf.springbootssm.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author manchi520
 * @date 2022/7/11
 * @apiNote
 */
@Controller
@Slf4j
@ConfigurationProperties(prefix = "down")
public class DownloadController {
    private String exportdir;

    @PostMapping("/download")
    public void download(String filename, HttpServletRequest request, HttpServletResponse response){

        try {
            response.addHeader("content-disposition","attachment;filename="+filename);
            IOUtils.copy(new FileInputStream(exportdir+filename),response.getOutputStream());
            log.debug("下载文件成功:filepath={}",exportdir+filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
